package com.bdc.activity;

import java.util.List;

import com.bdc.R;
import com.bdc.biz.MemorizeBiz;
import com.bdc.biz.MemorizeItem;
import com.bdc.domain.RecordInfo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Button;

public class MemorizeActivity extends Activity {
	
	private Button showAnswerButton ;
	private Button notRememberButton ;
	private Button blurButton ;
	private Button rememberButton ;
	private TextView answerText ;
	private TextView hintText ;
	private View maskImage ;
	private ProgressDialog dialog ;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.memorize);
        
        showAnswerButton = (Button)findViewById(R.id.showAnswerButton) ;
        notRememberButton = (Button)findViewById(R.id.notRememberButton) ;
        blurButton = (Button)findViewById(R.id.blurButton) ;
        rememberButton = (Button)findViewById(R.id.rememberButton) ;
        maskImage = findViewById(R.id.maskImage) ;
        answerText = (TextView)findViewById(R.id.answerText) ;
        hintText = (TextView)findViewById(R.id.hintText) ;
        
        showAnswerButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				maskImage.setVisibility(View.GONE) ;
				showAnswerButton.setVisibility(View.GONE) ;
				answerText.setVisibility(View.VISIBLE) ;
			}
		}) ;
        
        View.OnClickListener memorizeButtonListener = new View.OnClickListener() {
			public void onClick(View arg0) {
				getNextItem() ;
			}
		} ;
        
        notRememberButton.setOnClickListener(memorizeButtonListener) ;
        blurButton.setOnClickListener(memorizeButtonListener) ;
        rememberButton.setOnClickListener(memorizeButtonListener) ;
        
        getNextItem() ;
    }
    
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
        	if(msg.obj==null){
        		setContentView(R.layout.memorize_empty) ;
        	}else{
	        	MemorizeItem item = (MemorizeItem)msg.obj ;
	        	hintText.setText(item.getHint()) ;
	    		answerText.setText(item.getAnswer()) ;
        	}
    		dialog.dismiss() ;
    		maskImage.setVisibility(View.VISIBLE) ;
			showAnswerButton.setVisibility(View.VISIBLE) ;
			answerText.setVisibility(View.GONE) ;
        }
    };
    
    private void getNextItem(){
    	dialog = ProgressDialog.show(MemorizeActivity.this, "", 
                "Loading. Please wait...", true);
    	new ProgressThread(handler).start() ;
    }
    
    private class ProgressThread extends Thread{
    	private Handler h ;
    	
    	public ProgressThread(Handler h){
    		this.h = h ;
    	}
    	
    	public void run() {
			MemorizeItem item = MemorizeBiz.getNextItem() ;
			try {
				Thread.sleep(2000) ;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Message msg = h.obtainMessage() ;
			msg.obj = item ;
			h.sendMessage(msg) ;
		}
    }
}
